package com.pkk.components.util.email.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.pkk.components.util.YmlParserUtil;
import com.pkk.components.util.email.model.EmailProperties;


/**
 * @description: 加载基础邮箱配置
 * @author: peikunkun
 * @create: 2018-09-08 14:54
 **/
public class BaseEmailConfig {

    private static EmailProperties emailProperties = null;

    static {
        final String config = "config/base-email.yml";
        Resource resource = new ClassPathResource(config);
        InputStream in = null;
        try {
            in = resource.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        final Map<String, String> map = YmlParserUtil.parserYmlToProperties(in);
        emailProperties = new EmailProperties();
        if (map != null) {
            if (StringUtils.isNotBlank(map.get("commonutil.base-email.config.email.protocol"))) {
                emailProperties.setProtocol(map.get("commonutil.base-email.config.email.protocol"));
            }
            if (StringUtils.isNotBlank(map.get("commonutil.base-email.config.email.host"))) {
                emailProperties.setHost(map.get("commonutil.base-email.config.email.host"));
            }
            if (StringUtils.isNotBlank(map.get("commonutil.base-email.config.email.sendname"))) {
                emailProperties.setSendName(map.get("commonutil.base-email.config.email.sendname"));
            }
            if (StringUtils.isNotBlank(map.get("commonutil.base-email.config.email.sendport"))) {
                emailProperties.setSendPort(map.get("commonutil.base-email.config.email.sendport"));
            }
            if (StringUtils.isNotBlank(map.get("commonutil.base-email.config.email.username"))) {
                emailProperties.setUserName(map.get("commonutil.base-email.config.email.username"));
            }
            if (StringUtils.isNotBlank(map.get("commonutil.base-email.config.email.userpwd"))) {
                emailProperties.setUserPwd(map.get("commonutil.base-email.config.email.userpwd"));
            }
        }
    }


    /**
     * @Description: 获取发件人信息
     * @Param: []
     * @return: com.pkk.email.model.EmailProperties
     * @Author: peikunkun
     * @Date: 2018/9/8 0008 下午 3:16
     */
    public static EmailProperties getDefaultEmailConfig() {
        return emailProperties;
    }

    public static void main(String[] args) {
        System.out.println(emailProperties);
    }

}
